package rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author lumi
 * @date 2019/12/25 21:59
 */
public class Producer01_publish {
    private static final String QUEUE_INFORM_EMAIL = "QUEUE_INFORM_EMAIL";
    private static final String QUEUE_INFORM_SMS = "QUEUE_INFORM_SMS";
    private static final String EXCHANGE_FANOUT_INFORM = "EXCHANGE_FANOUT_INFORM";


    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        Connection connection = null;


        try {
            connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
             channel.queueDeclare(QUEUE_INFORM_EMAIL,true,false,false,null);
             channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);
             channel.exchangeDeclare(EXCHANGE_FANOUT_INFORM, BuiltinExchangeType.FANOUT);

             channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_FANOUT_INFORM,"");
             channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_FANOUT_INFORM,"");
             String msg = "Inform msg to usr";
             //发送消息
            channel.basicPublish(EXCHANGE_FANOUT_INFORM,"",null,msg.getBytes());

            System.out.println("send to mq msg"+ msg);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
